package cqrtplm.service.impl;

import com.ty.basic.entity.org.UserDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.entity.RtCertBaseCertDO;
import cqrtplm.entity.RtCertCombCertDO;
import cqrtplm.entity.RtTestRequirementsDO;
import cqrtplm.log.ObjectComparator;
import cqrtplm.mapper.ProcessNumberMapper;
import cqrtplm.mapper.RTLogDifferenceMapper;
import cqrtplm.mapper.RtCertCombCertMapper;
import cqrtplm.service.RtCertCombCertService;
import cqrtplm.util.RTLogHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 基础认证Service业务层处理
 *
 * @author licong
 * @date 2025-07-29
 */
@Service
@Slf4j
public class RtCertCombCertServiceImpl implements RtCertCombCertService
{
    @Autowired
    private RtCertCombCertMapper rtCertCombCertMapper;

    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    @Resource
    private RTLogHelper rtLogHelper;
    @Resource
    private RTLogDifferenceServiceImpl rtLogDifferenceService;
    /**
     * 查询基础认证
     *
     * @param id 基础认证主键
     * @return 基础认证
     */
    @Override
    public RtCertCombCertDO selectRtCertCombCertById(String id)
    {
        RtCertCombCertDO ret=rtCertCombCertMapper.selectRtCertCombCertById(id);
        return ret;
    }

    /**
     * 查询基础认证列表
     *
     * @param rtCertCombCert 基础认证
     * @return 基础认证
     */
    @Override
    public List<RtCertCombCertDO> selectRtCertCombCertList(RtCertCombCertDO rtCertCombCert)
    {
        return rtCertCombCertMapper.selectRtCertCombCertList(rtCertCombCert);
    }

    /**
     * 新增基础认证
     *
     * @param rtCertCombCert 基础认证
     * @return 结果
     */
    @Override
    public int insertRtCertCombCert(RtCertCombCertDO rtCertCombCert)
    {
        if(rtCertCombCert.getCertname()==null||rtCertCombCert.getCertname().isEmpty())
            throw new RuntimeException("请填写组合认证名称");
        if(rtCertCombCert.getCertcode()==null||rtCertCombCert.getCertcode().isEmpty())
             throw new RuntimeException("请填写组合认证代号");

        String name=rtCertCombCert.getCertname();
        String[] nameArray = name.split("\\+");
        List<String> names = Arrays.asList(nameArray);
        names.sort(String::compareTo);
        rtCertCombCert.setSortedname(String.join(",", names));
        rtCertCombCert.setCertcode(rtCertCombCert.getCertcode().toUpperCase());

        List<RtCertCombCertDO> oldlist=rtCertCombCertMapper.selectExistsRtCertCombCertList(rtCertCombCert);
        if(!oldlist.isEmpty()){
            //throw new RuntimeException("该组合认证名称或代号已存在,请勿重复添加");
            throw RTErrorCodeEnum.CERT_NAME_EXISTS.getException("该组合认证名称或代号已存在,请勿重复添加");
        }else{
            UserDO user = TyAccountContext.getUser();
            if (user == null) {
                //return ResponseResult.fail("401", "用户未登录");
                throw new RuntimeException("用户未登录");
            }
            String snowId=  String.valueOf(snowflakeIdComponent.getInstance().nextId());
            Timestamp timestamp = new Timestamp(new Date().getTime());

            rtCertCombCert.setId(snowId);
            rtCertCombCert.setCreatetime(timestamp);
            rtCertCombCert.setCreateuserid(String.valueOf(user.getOid()));
            rtCertCombCert.setCreateusername(user.getName());


//            log.info("组合认证-新增{},{},{},{}",
//                     rtCertCombCert.getCertname(),
//                     snowId,
//                     timestamp,
//                     user.getName());
            rtLogDifferenceService.newLog(
                    rtCertCombCert,null,
                    rtCertCombCert.getId(),
                    rtCertCombCert.getCreateusername(),
                    "表单",
                    "新增",
                    ""
            );
            return rtCertCombCertMapper.insertRtCertCombCert(rtCertCombCert);
        }

    }

    /**
     * 修改基础认证
     *
     * @param rtCertCombCert 基础认证
     * @return 结果
     */
    @Override
    public int updateRtCertCombCert(RtCertCombCertDO rtCertCombCert)
    {
        RtCertCombCertDO old=rtCertCombCertMapper.selectRtCertCombCertById(rtCertCombCert.getId());
        if(old==null){
            throw RTErrorCodeEnum.DATA_NOT_EXISTS.getException("该组合认证不存在,请检查");
        }else{
            UserDO user = TyAccountContext.getUser();
            if (user == null) {
                //return ResponseResult.fail("401", "用户未登录");
                throw new RuntimeException("用户未登录");
            }

            Timestamp timestamp = new Timestamp(new Date().getTime());
            rtCertCombCert.setUpdatetime(timestamp);
            rtCertCombCert.setUpdateuserid(String.valueOf(user.getOid()));
            rtCertCombCert.setUpdateusername(user.getName());

            int r=rtCertCombCertMapper.updateRtCertCombCert(rtCertCombCert);
            if(r>0){
                rtLogDifferenceService.newLog(
                        rtCertCombCert,old,
                        rtCertCombCert.getId(),
                        rtCertCombCert.getCreateusername(),
                        "表单",
                        "修改",
                        ""
                );
            }
            return r;
        }


    }

    /**
     * 批量删除基础认证
     *
     * @param ids 需要删除的基础认证主键
     * @return 结果
     */
    @Override
    public int deleteRtCertCombCertByIds(List<String> ids)
    {
        return rtCertCombCertMapper.deleteRtCertCombCertByIds(ids);
    }

    /**
     * 删除基础认证信息
     *
     * @param id 基础认证主键
     * @return 结果
     */
    @Override
    public int deleteRtCertCombCertById(String id)
    {
        return rtCertCombCertMapper.deleteRtCertCombCertById(id);
    }

    /**
     * @return 所有认证类型
     */
    @Override
    public List<String> queryAll() {
        List list = new ArrayList<>();
        List<RtCertCombCertDO> rtCertCombCertDOS = rtCertCombCertMapper.queryAll();
        if(rtCertCombCertDOS.size()>0){
            for(RtCertCombCertDO rtCertCombCertDO:rtCertCombCertDOS){
                list.add(rtCertCombCertDO.getCertname());
            }
        }
        return list;
    }
}




